package mcom.cmutweet;

import java.util.ArrayList;
import java.util.List;

import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import org.json.simple.JSONValue;

public class LogJSON {
	public static final int TYPE_PUBLISH = 0;
	public static final int TYPE_DISCARD = 1;
	public static final int TYPE_DELETE = 2;

	public static final int NUDGE_NONE = 0;
	public static final int NUDGE_TIMERICON = 1;
	
	public int type;
	public int nudge;
	public boolean timerShown;
	public boolean retweet;
	public boolean location;
	public boolean image;
	public String text;
	public List<Event> events;
	
	public LogJSON(int type, int nudge, boolean retweet, boolean location) {
		this.nudge = nudge;
		this.retweet = retweet;
		this.location = location;
		
		this.timerShown = false;
		this.image = false;
		this.text = "";
		this.events = new ArrayList<Event>();
	}
	public LogJSON(String jsonStr){
		JSONObject obj = (JSONObject)JSONValue.parse(jsonStr);
		this.type = (Integer)obj.get("type");
		this.nudge = (Integer)obj.get("nudge");
		this.timerShown = (Boolean)obj.get("timerShown");
		this.retweet = (Boolean)obj.get("retweet");
		this.location = (Boolean)obj.get("location");
		this.image = (Boolean)obj.get("image");
		this.text = (String)obj.get("text");
		this.events = new ArrayList<Event>();
		JSONArray array = (JSONArray)obj.get("events");
		if(array != null && array.size()>0){
			for(int i = 0;i < array.size();++i){
				JSONObject objj = (JSONObject)array.get(i);
				Event event = new Event(objj);
				this.events.add(event);
			}
		}
	}
	public String toJSONStr(){
		JSONObject obj = new JSONObject();
		obj.put("type", new Integer(type));
		obj.put("nudge", new Integer(nudge));
		obj.put("timerShown", new Boolean(timerShown));
		obj.put("retweet",new Boolean(retweet));
		obj.put("location", new Boolean(location));
		obj.put("image",new Boolean(image));
		obj.put("text",text);
		JSONArray array = new JSONArray();
		for(Event event:events){
			JSONObject objj = event.toJSON();
			array.add(objj);
		}
		obj.put("events", array);
		return obj.toString();
	}
	
	public void addEvent(int event_type, long timestamp, String text, String metadata){
		switch(event_type){
		case Event.EVENT_EDIT_TWEET:
			this.type = TYPE_PUBLISH;
			this.text = text;
			break;
		case Event.EVENT_EDIT_CANCEL:
			this.type = TYPE_DISCARD;
			this.text = text;
			break;
		case Event.EVENT_DELETE:
			this.type = TYPE_DELETE;
			this.text = text;
			break;
			
		case Event.EVENT_TIMER_START:
			this.timerShown = true;
			break;
		case Event.EVENT_LOCATION_ON:
			this.location = true;
			break;
		case Event.EVENT_LOCATION_OFF:
			this.location = false;
			break;
		case Event.EVENT_IMAGE_ON:
			this.image = true;
			break;
		case Event.EVENT_IMAGE_OFF:
			this.image = false;
			break;
		}
		Event event = new Event(event_type, timestamp, text, metadata);
		this.events.add(event);
	}

	public static class Event{
		public static final int EVENT_EDIT_START = 0;
		public static final int EVENT_EDIT_TWEET = 1;
		public static final int EVENT_EDIT_CANCEL = 2;
		public static final int EVENT_TIMER_START = 3;
		public static final int EVENT_TIMER_FINISH = 4;
		public static final int EVENT_TIMER_BYPASS = 5;
		public static final int EVENT_TIMER_CANCEL = 6;
		public static final int EVENT_LOCATION_ON = 7;
		public static final int EVENT_LOCATION_OFF = 8;
		public static final int EVENT_IMAGE_ON = 9;
		public static final int EVENT_IMAGE_OFF = 10;
		public static final int EVENT_DELETE = 11;
		
		public int event_type;
		public long timestamp;
		public String text;
		public String metadata;
		
		public Event(int event_type, long timestamp, String text, String metadata) {
			this.event_type = event_type;
			this.timestamp = timestamp;
			this.text = text;
			this.metadata = metadata;
		}
		public Event(JSONObject obj){
			this.event_type = (Integer)obj.get("event_type");
			this.timestamp = (Long)obj.get("timestamp");
			this.text = (String)obj.get("text");
			this.metadata = (String)obj.get("metadata");
		}
		
		public JSONObject toJSON(){
			JSONObject obj = new JSONObject();
			obj.put("event_type", new Integer(event_type));
			obj.put("timestamp", new Long(timestamp));
			obj.put("text", text);
			obj.put("metadata", metadata);
			return obj;
		}
	}
	
	
}
